require('../css/add.less')
document.ready(function () {
    const baseUrl = 'http://139.9.177.51:5000';
    let imgurlDom = document.querySelector('#imgurl')
    let btnDom = document.querySelector('.btn')
    let imgInp = document.querySelector('#imgInp')
    let bookNameDom = document.querySelector('.bookName')
    let categoryDom = document.querySelector('.category')
    let descDom = document.querySelector('#desc')

    let params = {
        bookName: "",
        category: "",
        desc: '',
        imgurl: '',
      }
    


    imgInp.addEventListener('change', function () {
        // console.log(this.files[0]);
        $updateFile('/book/upload', 'imgurl', this.files[0], function (res) {
            // console.log(res.imgurl);
            let url = baseUrl + res.imgurl;
            imgurlDom.src = url;
            params.imgurl = url
            console.log(res);
        })
    })
    
    
    // imgurlDom.addEventListener('click', function (e) {
    //     imgInp.click();
    //     e.stopPropagation();
    // })




    // $http.post('/book/upload',{ imgurl: url }, function (res) {
    //     console.log(res);
    // let url = baseUrl + res.data;

    // })



    btnDom.addEventListener('click', function () {
        // console.log(111);
        params.bookName =  bookNameDom.value
        params.category =  categoryDom.value
        params.desc =  descDom.value
        $http.post('/book/add',params,function(res){
            console.log(res);
        })
    })

})